home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / motif / otext.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  237 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * otext.c : openGL (motif) demo to draw bitmapped fonts using display lists
  25.  *
  26.  * Author : Yusuf Attarwala
  27.  *          SGI - Applications
  28.  *
  29.  * Date   : Feb 93
  30.  *
  31.  *---------------------------------------------------------------------------*/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. #include <Xm/Xm.h> 
  36. #include <Xm/Frame.h>               /* for frame widgets */
  37.  
  38. #include <GL/gl.h>                  /* gl includes */
  39. #include <GL/glu.h>                 /* utility library includes */
  40. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  41.  
  42. /* function declarations */
  43.  
  44. int 
  45.         main(int,char **);
  46. static void 
  47.             createToplevel(void),
  48.             drawScene(void),
  49.             setMatrix(void),
  50.             exposeCB(Widget, XtPointer, XtPointer),
  51.             resizeCB(Widget, XtPointer, XtPointer),
  52.             initCB(Widget, XtPointer, XtPointer);
  53.  
  54. void        makeFontSet(void),
  55.         myCharStr(char *);
  56.  
  57.  
  58. /* global variables */
  59.             
  60. Display       *display;             /* current display */
  61. XtAppContext  appContext;           /* X application context */
  62. GLuint        base;
  63.  
  64. Widget       toplevel,       /* toplevel shell */
  65.              glw;            /* current widget */
  66.              
  67. GLXContext   glxc;           /* current glx context */
  68.  
  69. char outputText[125];
  70. static  GLfloat yellow[3] = {1.0,1.0,0.0};
  71.  
  72. Arg args[20];
  73. int acnt;
  74.  
  75. int 
  76. main(int argc, char** argv)
  77. {
  78.     int i;
  79.     char oneWord[60];
  80.  
  81.     /* parse arguments, etc */
  82.     if (argc > 1) {
  83.     outputText[0] = '\0';
  84.     for (i=1;i<argc;i++) {
  85.         sprintf(oneWord,"%s ",argv[i]);
  86.         strcat(outputText,oneWord);
  87.     }
  88.     }
  89.     else {
  90.     fprintf(stderr,"Usage : %s string1 [string2...]\n",argv[0]);
  91.     strcpy(outputText,"HELLO WORLD");
  92.     }
  93.  
  94.     XtToolkitInitialize();
  95.     appContext = XtCreateApplicationContext();
  96.     display    = XtOpenDisplay(appContext, NULL, "Otext","otext",NULL,0,
  97.                               &argc,argv);
  98.     if (!display) {
  99.         printf("%s : Unable to open display\n",argv[0]);
  100.         exit(0);
  101.     }
  102.     createToplevel();             /* create and realize widget hierarchy */
  103.     XtAppMainLoop(appContext);    /* loop for ever */
  104. }
  105.  
  106.  
  107.  
  108. void
  109. createToplevel(void)
  110. {
  111.     Widget frame;
  112.  
  113.     acnt = 0;
  114.     XtSetArg(args[acnt],XmNminHeight, 200);acnt++;
  115.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  116.     toplevel  = XtAppCreateShell("openGL text","xtext",
  117.                                   applicationShellWidgetClass,
  118.                                   display,args,acnt);
  119.  
  120.     /* create a frame to hold the glx drawing area */
  121.     frame   = XtVaCreateManagedWidget("frame", 
  122.                   xmFrameWidgetClass, toplevel, 
  123.                   NULL);
  124.  
  125.     /* create and manage a single buffer widget, rgb mode */
  126.     acnt = 0;
  127.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  128.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  129.     XtManageChild(glw);
  130.  
  131.     /* register callbacks */
  132.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  133.  
  134.     /* realize widget hierarchy */
  135.     XtRealizeWidget(toplevel);
  136.  
  137.     /* generate display lists for text bitmaps */
  138.     makeFontSet();
  139.  
  140.     /* additional callbacks */
  141.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  142.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  143.  
  144. }
  145.  
  146. void
  147. drawScene(void)
  148. {
  149.     glClearColor(0.0, 0.0, 0.0, 0.0);
  150.     glClear(GL_COLOR_BUFFER_BIT);
  151.     glColor3fv(yellow);
  152.     glRasterPos2i(10,10);
  153.     myCharStr(outputText);
  154.     glFlush();
  155. }
  156.  
  157. static void
  158. setMatrix(void)
  159. {
  160.     glMatrixMode(GL_PROJECTION);
  161.     glLoadIdentity();
  162.     glOrtho(0.0,100.0,0.0,100.0,-1.0,1.0); /* a simple ortho */
  163.     glMatrixMode(GL_MODELVIEW);
  164.     glLoadIdentity();                      /* default view */
  165. }
  166.  
  167. static void 
  168. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  169. {
  170.     GLwDrawingAreaCallbackStruct *call_data;
  171.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  172.  
  173.     GLwDrawingAreaMakeCurrent(w, glxc);
  174.     glViewport(0, 0, call_data->width, call_data->height);
  175.     setMatrix();
  176.     drawScene();
  177. }
  178.  
  179. static void 
  180. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  181. {
  182.     GLwDrawingAreaCallbackStruct *call_data;
  183.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  184.  
  185.     GLwDrawingAreaMakeCurrent(w, glxc);
  186.     drawScene();
  187. }
  188.  
  189. static void
  190. initCB(Widget w, XtPointer client_data, XtPointer call)
  191. {
  192.     XVisualInfo *vi;
  193.  
  194.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  195.     XtGetValues(w, args, 1);
  196.  
  197.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  198. }
  199.  
  200. void
  201. makeFontSet()
  202. {
  203.     XFontStruct *xfont;
  204.     unsigned int firstChar, lastChar;
  205.  
  206.     GLwDrawingAreaMakeCurrent(glw, glxc);
  207.  
  208.     xfont = XLoadQueryFont(display,"9x15");
  209.     /* replace 9x15 with your favorite font set */
  210.  
  211.     if (!xfont) {
  212.     fprintf(stderr,"FONT NOT FOUND \n");
  213.     exit(0);
  214.     }
  215.  
  216.     firstChar = xfont->min_char_or_byte2;
  217.     lastChar  = xfont->max_char_or_byte2;
  218.  
  219.     if (base = glGenLists(lastChar-firstChar+1)) {
  220.     glXUseXFont(xfont->fid,firstChar,lastChar-firstChar+1,base+firstChar);
  221.     }
  222.     else {
  223.     fprintf(stderr,"OUT OF DISPLAY LISTS\n");
  224.     exit(0);
  225.     }
  226. }
  227.  
  228. void
  229. myCharStr(char *s)
  230. {
  231.     glPushAttrib(GL_LIST_BIT);
  232.     glListBase(base);
  233.     glCallLists(strlen(s),GL_UNSIGNED_BYTE,(unsigned char *)s);
  234.     glPopAttrib();
  235. }
  236.  
  237.